<template>
    <el-card class="box-card" style="margin:10px 0">
        <div slot="header" class="clearfix">
            <!-- 头部左侧 -->
            <el-tabs class="tab" v-model="activeName">
                <el-tab-pane label="销售额" name="sale"></el-tab-pane>
                <el-tab-pane label="访问量" name="visit"></el-tab-pane>
            </el-tabs>
            <!-- 头部右侧 -->
            <div class="right">
                <span @click="setDay">今日</span>
                <span @click="setWeek">本周</span>
                <span @click="setMonth">本月</span>
                <span @click="setYear">本年</span>
                <el-date-picker v-model="date" class="data" type="daterange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期" value-format="yyyy-MM-dd">
                </el-date-picker>
            </div>
        </div>
        <div>
            <el-row :gutter="10">
                <el-col :span="18">
                    <!-- 存放echarts -->
                    <div class="charts" ref="charts">

                    </div>
                </el-col>
                <el-col :span="6" class="right">
                    <h3>门店{{ title }}排名</h3>
                    <ul>
                        <li class="list">
                            <span class="rindex">1</span>
                            <span>蒸羊羔</span>
                            <span class="rvalue">451241</span>
                        </li>
                        <li>
                            <span class="rindex">2</span>
                            <span>蒸熊掌</span>
                            <span class="rvalue">112465</span>
                        </li>
                        <li>
                            <span class="rindex">3</span>
                            <span>蒸鹿尾</span>
                            <span class="rvalue">89892</span>
                        </li>
                        <li>
                            <span class="r2index">4</span>
                            <span>烧花鸭</span>
                            <span class="rvalue">12364</span>
                        </li>
                        <li>
                            <span class="r2index">5</span>
                            <span>烧雏鸡</span>
                            <span class="rvalue">6424</span>
                        </li>
                        <li>
                            <span class="r2index">6</span>
                            <span>烧子鹅</span>
                            <span class="rvalue">5453</span>
                        </li>
                        <li>
                            <span class="r2index">7</span>
                            <span>卤猪卤鸭</span>
                            <span class="rvalue">2345</span>
                        </li>

                    </ul>
                </el-col>
            </el-row>
        </div>
    </el-card>
</template>

<script>
// 引入echarts
import echarts from 'echarts'
//引入dayjs插件
import dayjs from 'dayjs'
export default {
    name: 'sale',
    data() {
        return {
            activeName: 'sale',
            myEcharts: null,
            //收集日历的数据
            date:''
        }
    },
    mounted() {
        this.myEcharts = echarts.init(this.$refs.charts);
        this.myEcharts.setOption({
            title: {
                text: '销售额趋势'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: 'Direct',
                    type: 'bar',
                    barWidth: '30%',
                    data: [101, 152, 200, 334, 390, 330, 220, 112, 213, 167, 209, 235, 123],
                    color: 'blue'
                }
            ]
        })

    },
    computed: {
        title() {
            return this.activeName == 'sale' ? '销售额' : '访问量'
        }
    },
    //监听属性
    watch: {
        title() {
            //重新修改图标的配置数据
            this.myEcharts.setOption({
                title: {
                    text: this.title
                }
            })
        }
    },
    methods: {
        //设置今日
        setDay(){
            const day = dayjs().format('YYYY-MM-DD');
            this.date= [day,day];
        },
        //设置本周
        setWeek(){
            const start = dayjs().day(1).format('YYYY-MM-DD');
            const end = dayjs().day(7).format('YYYY-MM-DD');
            this.date = [start,end]
        },
        //设置本月
        setMonth(){
            const start = dayjs().startOf('month').format('YYYY-MM-DD');
            const end = dayjs().endOf('month').format('YYYY-MM-DD');
            this.date = [start,end]
        },
        //设置本年
        setYear(){
            const start = dayjs().startOf('year').format('YYYY-MM-DD');
            const end = dayjs().endOf('year').format('YYYY-MM-DD');
            this.date = [start,end]
        }
    },
}
</script>

<style scoped>
.clearfix {
    position: relative;
    display: flex;
    justify-content: space-between;
}

.tab {
    width: 100%;
}

.right {
    position: absolute;
    right: 0;
}

.data {
    width: 240px;
    margin: 0 20px;
}

.right span {
    margin: 0 20px;
}

.charts {
    width: 100%;
    height: 300px;
}

ul {
    list-style: none;
    width: 100%;
    height: 300px;
    padding: 0;
}

ul li {
    height: 8%;
    margin: 10px 0;
}

.rindex {
    float: left;
    width: 22px;
    height: 22px;
    font-size: 20px;
    border-radius: 50%;
    background: black;
    color: white;
    text-align: center;
}

.rvalue {
    float: right;
}

.r2index {
    padding: 0 5px;
    font-size: 20px;
}
</style>